﻿#include "Communicator.h"

Communicator::Communicator(QObject *parent)
	: QObject(parent)
{
	init();
	connect(&m_serialPort, &QSerialPort::readyRead, [this]() {
		qDebug() << "---------------exe----------------";
		m_lock.lock();
		QByteArray recData = m_serialPort.readAll();
		qDebug() << "接收到硬件信号：" << recData;
		if (recData[0] == m_lastByteArray[0] && recData[1] == m_lastByteArray[1])
		{
			m_nowByteArr.clear();
			m_nowByteArr = recData;
		}else
		{
			m_nowByteArr.append(recData);
		}
		if (ifCrcCorrect())
		{
			switch (m_lastByteArray[2])
			{
			case 0x10:
				//解析电池信息
				m_processObj->processByteArrayOfBatteryInfo(m_nowByteArr);
				break;
			case 0x11:
				m_processObj->processByteArrayOfBatteryDetailInfo(m_nowByteArr);
				break;
			case 0x20:
				m_processObj->processByteArrayOfProtectParamInfo(m_nowByteArr);
			case  0x21:
				m_processObj->processByteArrayOfFactoryParamInfo(m_nowByteArr);
			default:
				break;
			}

		}
		m_lock.unlock();

		});
}

Communicator::~Communicator()
{
	delete m_processObj;
	delete m_processThread;
	delete m_cycleThread;
	delete m_sendWorker;
}

quint16 Communicator::calculateCRC16(const QByteArray &data)
{
	quint16 crc = 0xFFFF;
	QDataStream stream(data);
	while (!stream.atEnd()) {
		quint8 byte;
		stream >> byte;
		crc ^= (quint16)byte;
		for (int i = 0; i < 8; ++i) {
			if (crc & 0x0001) {
				crc >>= 1;
				crc ^= 0xA001;
			}
			else {
				crc >>= 1;
			}
		}
	}
	return crc;
}

void Communicator::writeData(QByteArray data)
{
	m_serialPort.write(data);
	m_serialPort.flush();
}

void Communicator::setLastByteArr(QByteArray arr)
{
	m_lastByteArray = arr;
}

//BatteryInfoRom Communicator::getBatteryInfo()
//{
//	sendReadBatteryInfoByteArray();
//}

void Communicator::setSerialPort()
{
	CoreData &coreData = CoreData::getInstance();
	SerialPortInfo info = coreData.m_serialPortInfo;
	m_serialPort.setPortName(info.portname);
	m_serialPort.setBaudRate(info.baudRate);
	m_serialPort.setDataBits(QSerialPort::Data8);
	m_serialPort.setStopBits(QSerialPort::OneStop);
	m_serialPort.setFlowControl(QSerialPort::NoFlowControl);
	m_serialPort.setParity(QSerialPort::NoParity);
	if (m_serialPort.isOpen())
	{
		m_serialPort.close();
	}
	if (m_serialPort.open(QIODevice::ReadWrite))
	{
		qDebug() << "Serial Port Opened Successfully......";
		emit connectSuccess();
		m_sendWorker = new SendMsgWorker(m_sendMsgVec, m_serialPort);
		initCycleRead();
		//connect(m_processThread, &QThread::started, m_sendWorker, &SendMsgWorker::sendInitData);
		m_sendWorker->moveToThread(m_cycleThread);
		m_cycleThread->start();

		
		//sendReadBatteryInfoByteArray();

		//sendReadBatteryDetailInfoByteArray();

		//sendReadProtectParamInfoByteArray();
		return;
	}
	else
	{
		qDebug() << "ERROR TO OPEN SERIAL PORT!!!";
		return;
	}

}

void Communicator::testThread()
{
	for (int i = 0;i<50;i++)
	{
		qDebug() << "-------------test---------------";
	}
	
}

void Communicator::startCycleThreadTimer()
{
	m_sendWorker->startTimer(4000);
}

//void Communicator::cycleWork()
//{
//	sendReadBatteryInfoByteArray();
//	sendReadProtectParamInfoByteArray();
//}

void Communicator::openSerialPort()
{
	if (m_serialPort.isOpen())
	{
		m_serialPort.close();
	}
	if (m_serialPort.open(QIODevice::ReadWrite))
	{
		qDebug() << "Serial Port Opened Successfully......";
		return;
	}
	else
	{
		qDebug() << "ERROR TO OPEN SERIAL PORT!!!";
		return;
	}
}

void Communicator::closeSerialPort()
{
	if (m_serialPort.isOpen())
	{
		m_serialPort.close();
	}
}

void Communicator::sendReadBatteryInfoByteArray()
{
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x03));
	sendByteInfo.append(static_cast<char>(0x10));
	sendByteInfo.append(static_cast<char>(0x00));
	sendByteInfo.append(static_cast<char>(0x00));
	sendByteInfo.append(static_cast<char>(0x19));
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(static_cast<char>(crc16 & 0xFF));
	sendByteInfo.append(static_cast<char>((crc16 >> 8) & 0xFF));
	

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
	m_serialPort.flush();
}

void Communicator::sendReadBatteryDetailInfoByteArray()
{
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x03));
	sendByteInfo.append(static_cast<char>(0x11));
	sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(static_cast<char>(0x02));
	sendByteInfo.append(static_cast<char>(0x58));	
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(static_cast<char>(crc16 & 0xFF));
	sendByteInfo.append(static_cast<char>((crc16 >> 8) & 0xFF));
	

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
	m_serialPort.flush();
}

void Communicator::sendReadProtectParamInfoByteArray()
{
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x03));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x00));
	sendByteInfo.append(static_cast<char>(0x00));
	sendByteInfo.append(static_cast<char>(0x4A));
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(static_cast<char>(crc16 & 0xFF));
	sendByteInfo.append(static_cast<char>((crc16 >> 8) & 0xFF));


	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
	m_serialPort.flush();
}

void Communicator::sendCHG_CMD_CloseByteArray()
{
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x30));
	sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(static_cast<char>(0x00));
	sendByteInfo.append(static_cast<char>(0xA5));
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(static_cast<char>(crc16 & 0xFF));
	sendByteInfo.append(static_cast<char>((crc16 >> 8) & 0xFF));


	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
	m_serialPort.flush();
}

void Communicator::sendCHG_CMD_OpenByteArray()
{
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x30));
	sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(static_cast<char>(0x00));
	sendByteInfo.append(static_cast<char>(0x5A));
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(static_cast<char>(crc16 & 0xFF));
	sendByteInfo.append(static_cast<char>((crc16 >> 8) & 0xFF));


	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
	m_serialPort.flush();
}

void Communicator::sendDSG_CMD_CloseByteArray()
{
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x30));
	sendByteInfo.append(static_cast<char>(0x02));
	sendByteInfo.append(static_cast<char>(0x00));
	sendByteInfo.append(static_cast<char>(0x5A));
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(static_cast<char>(crc16 & 0xFF));
	sendByteInfo.append(static_cast<char>((crc16 >> 8) & 0xFF));


	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
	m_serialPort.flush();
}

void Communicator::sendDSG_CMD_OpenByteArray()
{
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x30));
	sendByteInfo.append(static_cast<char>(0x02));
	sendByteInfo.append(static_cast<char>(0x00));
	sendByteInfo.append(static_cast<char>(0xA5));
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(static_cast<char>(crc16 & 0xFF));
	sendByteInfo.append(static_cast<char>((crc16 >> 8) & 0xFF));


	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
	m_serialPort.flush();
}

void Communicator::sendRESET_CMDByteArray()
{
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x30));
	sendByteInfo.append(static_cast<char>(0x04));
	sendByteInfo.append(static_cast<char>(0x00));
	sendByteInfo.append(static_cast<char>(0x5A));
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(static_cast<char>(crc16 & 0xFF));
	sendByteInfo.append(static_cast<char>((crc16 >> 8) & 0xFF));


	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
	m_serialPort.flush();
}

void Communicator::sendERASEFLASH_CMDByteArray()
{
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x30));
	sendByteInfo.append(static_cast<char>(0x05));
	sendByteInfo.append(static_cast<char>(0x00));
	sendByteInfo.append(static_cast<char>(0x5A));
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(static_cast<char>(crc16 & 0xFF));
	sendByteInfo.append(static_cast<char>((crc16 >> 8) & 0xFF));


	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
	m_serialPort.flush();
}

QByteArray Communicator::getBatteryInfoByteArray()
{
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x03));
	sendByteInfo.append(static_cast<char>(0x10));
	sendByteInfo.append(static_cast<char>(0x00));
	sendByteInfo.append(static_cast<char>(0x00));
	sendByteInfo.append(static_cast<char>(0x19));
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(static_cast<char>(crc16 & 0xFF));
	sendByteInfo.append(static_cast<char>((crc16 >> 8) & 0xFF));
	return sendByteInfo;
}

QByteArray Communicator::getBatteryDetailInfoByteArray()
{
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x03));
	sendByteInfo.append(static_cast<char>(0x11));
	sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(static_cast<char>(0x02));
	sendByteInfo.append(static_cast<char>(0x58));
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(static_cast<char>(crc16 & 0xFF));
	sendByteInfo.append(static_cast<char>((crc16 >> 8) & 0xFF));
	return sendByteInfo;
}

QByteArray Communicator::getProtectParamInfoByteArray()
{
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x03));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x00));
	sendByteInfo.append(static_cast<char>(0x00));
	sendByteInfo.append(static_cast<char>(0x4A));
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(static_cast<char>(crc16 & 0xFF));
	sendByteInfo.append(static_cast<char>((crc16 >> 8) & 0xFF));
	return sendByteInfo;
}

QByteArray Communicator::getFactoryParamInfoByteArray()
{
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x03));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x00));
	sendByteInfo.append(static_cast<char>(0x00));
	sendByteInfo.append(static_cast<char>(0x5F));
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(static_cast<char>(crc16 & 0xFF));
	sendByteInfo.append(static_cast<char>((crc16 >> 8) & 0xFF));
	return sendByteInfo;
}


void Communicator::updateBMS(QString filePath)
{
	QFile updateFile(filePath);
	if (updateFile.open(QIODevice::ReadOnly)) {
		int fileSize = updateFile.size();
		updateFile.close();
		m_configFilePath = filePath;
		m_configFileSize = fileSize;
		qDebug() << "File size:" << fileSize << "bytes";
		sendLengthData(fileSize,5);
	}
	else {
		qDebug() << "Failed to open file:" << updateFile.errorString();
	}
}

void Communicator::sendLengthData(int dataLength,int index)
{
	if (index ==0)
	{
		qDebug() << "Update Failed......";
		return;
	}
	if (dataLength > 0xFFFFFFFF)
	{
		qDebug() << "File size exceeds 4 bytes limit.";
		return;
	}
	else {
		QByteArray sizeBytes,sumBytes;
		sizeBytes.append(static_cast<char>((dataLength >> 24) & 0xFF));  // 最高位字节
		sizeBytes.append(static_cast<char>((dataLength >> 16) & 0xFF));  // 第二高位字节
		sizeBytes.append(static_cast<char>((dataLength >> 8) & 0xFF));   // 第三高位字节
		sizeBytes.append(static_cast<char>(dataLength & 0xFF));          // 最低位字节
		qDebug() << "File size:" << dataLength << "bytes";
		qDebug() << "File size as QByteArray:" << sizeBytes.toHex();
		sumBytes.append(static_cast<char>(0x00));
		sumBytes.append(sizeBytes);
		quint16 crc16 = calculateCRC16(sumBytes);
		sumBytes.append(crc16);
		m_serialPort.write(sumBytes);
		m_serialPort.flush();
		QByteArray returnBytes = m_serialPort.readAll();
		if (returnBytes[0] == static_cast<char>(0x01))
		{
			qDebug() << "send data length success!!!";
			sendUpdateFileData();
		}
		if (returnBytes[0] == static_cast<char>(0x02))
		{
			sendLengthData(dataLength, --index);
		}
		if (returnBytes[0] == static_cast<char>(0x03))
		{
			qDebug() << "transport data error!!!";
			return;
		}
	}
}

void Communicator::sendUpdateFileData()
{
	QFile file(m_configFilePath);
	if (!file.open(QIODevice::ReadWrite)) {
		qDebug() << "Failed to open file:" << file.errorString();
		return ;
	}
	int num = 0;
	int times = 5;
	while (!file.atEnd()) {
		sendUpdateFileData(times, num,file);
		//QByteArray sumBytes;
		//sumBytes.append(static_cast<char>(0x01));
		//sumBytes.append(static_cast<char>(num & 0xFF));
		//QByteArray data = file.read(1024);  // 读取文件数据块
		//sumBytes.append(data);
		//quint16 crc16 = calculateCRC16(sumBytes);
		//sumBytes.append(crc16);
		//qint64 bytesWritten = m_serialPort.write(sumBytes);
		//if (bytesWritten == -1) {
		//	qDebug() << "Failed to write to serial port:" << m_serialPort.errorString();
		//	break;
		//}
		//m_serialPort.flush();
		//QByteArray returnBytes = m_serialPort.readAll();
		//if (returnBytes[0] == static_cast<char>(0x01))
		//{
		//	num++;
		//}
		//if (returnBytes[0] == static_cast<char>(0x02))
		//{
		//}
		//if (returnBytes[0] == static_cast<char>(0x03))
		//{
		//	qDebug() << "Stop Transport,File Error!!!";
		//	return;
		//}
	}

	file.close();
}

void Communicator::sendUpdateFileData(int &times,int &num ,QFile &file)
{
	if (times < 1)
	{
		qDebug() << "Update Failed......";
		return;
	}
	QByteArray sumBytes;
	sumBytes.append(static_cast<char>(0x01));
	sumBytes.append(static_cast<char>(num & 0xFF));
	QByteArray data = file.read(1024);  // 读取文件数据块
	sumBytes.append(data);
	quint16 crc16 = calculateCRC16(sumBytes);
	sumBytes.append(crc16);
	qint64 bytesWritten = m_serialPort.write(sumBytes);
	if (bytesWritten == -1) {
		qDebug() << "Failed to write to serial port:" << m_serialPort.errorString();
	}
	m_serialPort.flush();
	QByteArray returnBytes = m_serialPort.readAll();
	if (returnBytes[0] == static_cast<char>(0x01))
	{
		num++;
		times = 5;
		emit updateSchedule((1024 / m_configFileSize)*num*100);
		return;
	}
	if (returnBytes[0] == static_cast<char>(0x02))
	{
		sendUpdateFileData(--times,num, file);
	}
	if (returnBytes[0] == static_cast<char>(0x03))
	{
		qDebug() << "Stop Transport,File Error!!!";
		return;
	}
}

void Communicator::writeTOV(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x00));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeTOVDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeTOVR(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x02));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeTOVRDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x03));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOV(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x04));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOVDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x05));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOVR(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOVRDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x07));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeTUV(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x08));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeTUVDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x09));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeTUVR(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x10));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeTUVRDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x11));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUV(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x12));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUVDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x13));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUVR(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x14));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUVRDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x15));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOTD(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x16));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOTDDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x17));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOTDR(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x18));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOTDRDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x19));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOTC(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x20));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOTCDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOTCR(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x22));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOTCRDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUTD(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x24));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUTDDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x25));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUTDR(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x26));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUTDRDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x27));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUTC(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x28));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUTCDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x29));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUTCR(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x30));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUTCRDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x31));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOCD1(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x32));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOCD1Delay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x33));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOCD1R(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x34));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOCD1RDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x35));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOCC(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x36));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOCCDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x37));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOCCR(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x38));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOCCRDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x39));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOCD2(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x40));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOCD2Delay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x41));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOCD2R(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x42));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOCD2RDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x43));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeSC(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x44));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOTMOS(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x46));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOTMOSDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x47));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOTMOSR(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x48));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOTMOSRDelay(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x23));
	sendByteInfo.append(static_cast<char>(0x49));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUART1_ADDR(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x00));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUART1_Bound(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUART2_ADDR(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x02));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUART2_Bound(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x03));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUART3_ADDR(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x04));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUART3_Bound(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x05));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUART4_ADDR(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeUART4_Bound(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x07));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeCAN1_BPS(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x08));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeCAN2_BPS(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x09));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeCAN3_BPS(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x0A));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeCAN4_BPS(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x0B));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writePACK_IterationFcc(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x10));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeCHG_Voltage(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x11));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeCHG_Current(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x12));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeREALRC_H(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x13));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeREALRC_L(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x14));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeOFFECT_I(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x15));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeSampRes(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x16));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeHOT_MODE(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x17));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeHOT_ON_TEMP(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x18));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeHOT_OFF_TEMP(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x19));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeCHG_DSG_MODE(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x1A));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeKEY_MODE(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x1B));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeCD_ForceTime(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x1C));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeBattery_Type(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x1D));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeBal_Res(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x0E));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeBal_Type(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x0F));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeBal_ONVol(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x20));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeBal_Vdiff(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

void Communicator::writeLowPower_BEEP_EN(int data)
{
	QByteArray byteArr;
	byteArr.append(static_cast<uchar>((data >> 8) & 0xFF)); // 高位
	byteArr.append(static_cast<uchar>(data & 0xFF));
	QByteArray sendByteInfo;
	//sendByteInfo.append(static_cast<char>(0x01));
	sendByteInfo.append(CoreData::_nowStationAddress);
	sendByteInfo.append(static_cast<char>(0x06));
	sendByteInfo.append(static_cast<char>(0x21));
	sendByteInfo.append(static_cast<char>(0x22));
	sendByteInfo.append(byteArr);
	quint16 crc16 = calculateCRC16(sendByteInfo);
	sendByteInfo.append(crc16);

	m_lastByteArray = sendByteInfo;
	m_serialPort.write(sendByteInfo);
}

bool Communicator::ifCrcCorrect()
{
	int length = m_nowByteArr.size();
	QByteArray actualData = m_nowByteArr.mid(0, length-2);
	quint16 correctCrc = calculateCRC16(actualData);
	//quint16 receivedCrc16 = static_cast<quint16>(m_nowByteArr[length -2]) << 8 |
	//	static_cast<quint16>(m_nowByteArr[length -1]);
	quint16 receivedCrc16 = static_cast<quint16>(m_nowByteArr[length - 2]) | static_cast<quint16>(m_nowByteArr[length - 1]) << 8;
	if (correctCrc != receivedCrc16) {
		return false; // CRC16 doesn't match
	}

	return true; // Data is valid

}

void Communicator::init()
{
	m_processObj = new DataProcess();
	m_cycleThread = new QThread();
	m_processThread = new QThread();
	m_processObj->moveToThread(m_processThread);
	m_processThread->start();
	connect(m_processObj, &DataProcess::readBatteryInfoFinished, [this]() {
		sendReadBatteryDetailInfoByteArray();
		});
	connect(m_processObj, &DataProcess::sendProtectParamInfo, [this](ProtectParamInfo protectParamInfo) {
		emit sendInitProtectParamInfo(protectParamInfo);
		});
	connect(m_processObj, &DataProcess::sendFactoryParamInfo, [this](FactoryParamInfo factoryParamInfo) {
		emit sendInitFactoryParamInfo(factoryParamInfo);
		});
	
	initSendMsgVec();
	//initCycleRead();

}

void Communicator::initSendMsgVec()
{
	m_sendMsgVec.append(getBatteryInfoByteArray());
	m_sendMsgVec.append(getBatteryDetailInfoByteArray());
	m_sendMsgVec.append(getProtectParamInfoByteArray());
	m_sendMsgVec.append(getFactoryParamInfoByteArray());
}

void Communicator::initCycleRead()
{
	connect(m_cycleThread, &QThread::started, m_sendWorker, &SendMsgWorker::sendInitData);
	connect(m_sendWorker, &SendMsgWorker::updateLastSendArr, this,&Communicator::setLastByteArr);
	connect(m_sendWorker, &SendMsgWorker::startCycleTimer, this, &Communicator::startCycleThreadTimer);
}
